package com.gjy.sb.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

import java.time.LocalTime;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-07-02 09:32:49
 */
public class MySpringApplicationRunListener implements SpringApplicationRunListener {

    private static final Logger log = LoggerFactory.getLogger(MySpringApplicationRunListener.class);

    private Long startTime;

    public MySpringApplicationRunListener(SpringApplication application, String[] args) {
    }

    @Override
    public void starting() {
        startTime = System.currentTimeMillis();
        log.info("MySpringListener启动开始 {}", LocalTime.now());
    }

    @Override
    public void environmentPrepared(ConfigurableEnvironment environment) {
        log.info("MySpringListener环境准备 准备耗时：{}毫秒", (System.currentTimeMillis() - startTime));
        startTime = System.currentTimeMillis();
    }

    @Override
    public void contextPrepared(ConfigurableApplicationContext context) {
        log.info("MySpringListener上下文准备 耗时：{}毫秒", (System.currentTimeMillis() - startTime));
        startTime = System.currentTimeMillis();
    }

    @Override
    public void contextLoaded(ConfigurableApplicationContext context) {
        log.info("MySpringListener上下文载入 耗时：{}毫秒", (System.currentTimeMillis() - startTime));
        startTime = System.currentTimeMillis();
    }

    @Override
    public void started(ConfigurableApplicationContext context) {
        log.info("MySpringListener started 耗时：{}毫秒", (System.currentTimeMillis() - startTime));
        startTime = System.currentTimeMillis();
    }

    @Override
    public void running(ConfigurableApplicationContext context) {
        log.info("MySpringListener running 耗时：{}毫秒", (System.currentTimeMillis() - startTime));
        startTime = System.currentTimeMillis();
    }

    @Override
    public void failed(ConfigurableApplicationContext context, Throwable exception) {
        log.info("MySpringListener failed 耗时：{}毫秒", (System.currentTimeMillis() - startTime));
        startTime = System.currentTimeMillis();
    }
}
